package com.hospital.register.user.controller.api;

import com.hospital.register.common.result.Result;
import com.hospital.register.common.utils.AuthContextHolder;
import com.hospital.register.constants.PatientConstants;
import com.hospital.register.dto.user.PatientQueryDto;
import com.hospital.register.model.user.Patient;
import com.hospital.register.user.service.PatientService;
import com.hospital.register.validation.groups.InsertValidation;
import com.hospital.register.vo.user.PatientQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "就诊人管理", description = "面向用户")
@RestController
@RequestMapping("/api/user/patient")
@Validated
public class PatientApiController {

    /**
     * 处理患者业务
     */
    @Autowired
    private PatientService patientService;

    @ApiOperation("查询当前用户其下的所有就诊人")
    @GetMapping("/auth/findAll")
    public Result<List<PatientQueryVo>> findAll(@ApiIgnore HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        PatientQueryDto conditions = new PatientQueryDto();
        conditions.setUserId(userId);
        List<PatientQueryVo> patientList = this.patientService.selectList(conditions);
        return Result.ok(patientList);
    }

    @ApiOperation("新增就诊人")
    @PostMapping("/auth/save")
    public Result savePatient(
            @RequestBody @Validated(value = InsertValidation.class) Patient patient,
            @ApiIgnore HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patient.setStatus(PatientConstants.STATUS_DEFAULT);
        this.patientService.insert(patient);
        return Result.ok();
    }

    @ApiOperation("根据 id 查询就诊人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "就诊人的 id", paramType = "path", dataTypeClass = Long.class, required = true)
    })
    @GetMapping("/auth/get/{id}")
    public Result<PatientQueryVo> getMyPatient(
            @PathVariable @NotNull(message = "就诊人 id 不可为空") @Min(value = 0L, message = "id 非法") Long id,
            @ApiIgnore HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        return Result.ok(this.patientService.selectMineById(id, userId));
    }

    @ApiOperation("修改就诊人信息")
    @PutMapping("/auth/update")
    public Result updatePatient(
            @RequestBody @Validated Patient patient,
            @ApiIgnore HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        this.patientService.update(patient);
        return Result.ok();
    }

    @ApiOperation("根据 id 删除就诊人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "就诊人的 id", paramType = "path", dataTypeClass = Long.class, required = true)
    })
    @DeleteMapping("/auth/remove/{id}")
    public Result removePatient(
            @PathVariable @NotNull(message = "就诊人 id 不可为空") @Min(value = 0, message = "就诊人 id 非法") Long id,
            @ApiIgnore HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        this.patientService.deleteById(id, userId);
        return Result.ok();
    }

    @ApiOperation("根据 id 查询就诊人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "就诊人的 id", paramType = "path", dataTypeClass = Long.class, required = true)
    })
    @GetMapping("/inner/get/{id}/{userId}")
    public PatientQueryVo getPatientById(
            @PathVariable @NotNull(message = "就诊人 id 不可为空") @Min(value = 0L, message = "就诊人 id 非法") Long id,
            @PathVariable @NotNull(message = "用户 id 不可为空") @Min(value = 0L, message = "用户 id 非法") Long userId) {
        return this.patientService.selectMineById(id, userId);
    }

}
